package cn.iocoder.yudao.module.dify.controller.admin.dify;


import cn.iocoder.yudao.module.dify.api.vo.resp.BlockResponse;
import cn.iocoder.yudao.module.dify.api.vo.resp.StreamResponse;
import cn.iocoder.yudao.module.dify.service.DifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@RestController
@RequestMapping("/api/dify")
@RequiredArgsConstructor
@Tag(name = "管理后台 - ai接口测试")
public class DifyController {

    @Value("${spring.dify.key.test}")
    private String difyKey;

    @Resource
    private final DifyService difyService;

    @GetMapping("/block")
    @Operation(summary = "ai对话测试-阻塞模式")
    public String test1() {
        String query = "鲁迅和周树人什么关系？";
        BlockResponse blockResponse = difyService.blockingMessage(query, getLoginUserId(), difyKey);
        return blockResponse.getAnswer();
    }

    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "ai对话测试-流模式")
    public Flux<StreamResponse> test2() {
        String query = "鲁迅和周树人什么关系？";
        return difyService.streamingMessage(query, getLoginUserId(), difyKey);
    }


}
